﻿package com.tangchao.client.sdh.view.role
{
	import com.tangchao.client.sdh.definition.DFGetCard;
	
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Clock extends MovieClip
	{
		private var _mc:MovieClip;
		private var _num:int;
		private var _time:Timer;
		private var _func:Function;
		
		public function Clock()
		{
			mcBack.gotoAndStop(1);
			_mc=new MovieClip();
			addChild(_mc);
			_time=new Timer(1000);
			_time.addEventListener(TimerEvent.TIMER,onTimer);
			this.visible=false;
		}
		
		private function onTimer(e:TimerEvent)
		{
			if(_num == 3)
			{
				_mc.gotoAndStop(2);
			}
			
			if(_num>0)
			{
				_num--;
				num=_num;
			}			
			else
			{
				_mc.gotoAndStop(1);
				if(_func != null)
				{
					_func();
					_func = null;
				}
				stopTimer();
				
			}
		}
		
		public function setTimer(val:int, func:Function):void
		{
			_func = func;
			num = val;
			startTimer();
		}
		
		public function set num(value:int)
		{
			while(_mc.numChildren>0)
			{
				_mc.removeChildAt(0);
			}
			_num  = value;
			var vtos:String=value.toString();
			for(var i:int=0;i<vtos.length;i++)
			{
				var mc:MovieClip=DFGetCard.getNumByNum(int(vtos.substr(i,1)));
				_mc.addChild(mc);
				mc.x=mc.width/2*i;
			}
			
			if(_mc.numChildren==2)
			{
				_mc.x=0;
			}
			else if(_mc.numChildren==1)
			{
				_mc.x=10;
			}
		}
		
		public function startTimer()
		{
			_time.reset();
			_time.start();
			this.visible=true;
		}
		
		public function stopTimer()
		{
			_func = null;
			_time.stop();
			_mc.gotoAndStop(1);
			this.visible=false;
		}
	}
}